package com.xzq.bustack;

import com.xzq.bustack.netty.NettyServer;
import io.netty.channel.ChannelFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.InetSocketAddress;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/20 12:29
 * @Version 1.0.0
 **/
@SpringBootApplication
public class App implements CommandLineRunner {

    @Value("${netty.host}")
    private String host;
    @Value("${netty.port}")
    private int port;

    @Autowired
    private NettyServer nettyServer;

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        InetSocketAddress address = new InetSocketAddress(host, port);
        ChannelFuture channelFuture = nettyServer.lister(address);
        Runtime.getRuntime().addShutdownHook(new Thread(()->{nettyServer.destroy();}));
        channelFuture.channel().closeFuture().syncUninterruptibly();
    }
}
